import { _decorator, Component, math, Node, toRadian, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MonsterController')
export class MonsterController extends Component {
    public data_speed: number = 100
    public data_angle: number = 0
    public data_destAngle: number = 0
    public data_angleSpeed: number = 270

    public angelCD: number = 5
    public randomCountDown: number = 5

    public followTarget: Node = undefined

    start() {
        this.data_angle = math.randomRange(0, 360)
        this.data_destAngle = this.data_angle
        this.node.angle = this.data_angle
    }

    updateOuter(deltaTime: number) {
        this.angelCD -= deltaTime
        if (this.followTarget) { 
            this.follow()
        }
        else if (this.angelCD <= 0) {
            this.angelCD = this.randomCountDown
            this.randomAngle()
        }

        this.updateAngle(deltaTime)
        this.node.angle = this.data_angle

        const pos = this.node.position
        const addPos = this.getAddPosition(deltaTime)
        this.node.setPosition(pos.x + addPos.x, pos.y + addPos.y)
    }

    updateAngle(dt: number) {
        if (this.data_angle == this.data_destAngle) 
            return

        const dtAngle = this.data_angleSpeed * dt

        const offset = this.data_destAngle - this.data_angle
        let offsetLength = offset
        let sign = Math.sign(offset)
        if (Math.abs(offset) > 180) {
            sign *= -1
            offsetLength = offset > 180 ? 360 - offsetLength : 360 + offsetLength
        }
        else {
            offsetLength = Math.abs(offsetLength)
        }

        if (dtAngle > offsetLength) {
            this.data_angle = this.data_destAngle
        }
        else {
            const angle = dtAngle * sign + this.data_angle
            if (angle > 360) {
                this.data_angle = angle % 360
            }
            else if (angle < 0) {
                this.data_angle = angle + 360
            }
            else {
                this.data_angle = angle
            }
        }
    }

    follow() {
        const offset = new Vec2()
        const PI = 3.1415926
        if (this.followTarget) {
            offset.x = this.followTarget.position.x - this.node.position.x
            offset.y = this.followTarget.position.y - this.node.position.y
            offset.normalize()
            const cosO = offset.dot(new Vec2(0, 1))
            const radius = Math.acos(cosO)
            let angle = radius / PI * 180
            if (offset.x > 0) {
                angle = - Math.abs(angle) + 360
            }
            else {
                angle = Math.abs(angle)
            }
            this.data_destAngle = angle
        }
    }

    randomAngle() {
        this.data_destAngle = math.randomRange(0, 360)
    }

    getAddPosition(deltaTime: number) {
        let velocity = new Vec2(0, 1).rotate(toRadian(this.data_angle))
        const length = this.data_speed * deltaTime
        return velocity.multiplyScalar(length)
    }

    //outer
    setFollowTarget(target: Node) {
        if (target) {
            this.followTarget = target
            this.follow()

            this.data_angle = this.data_destAngle
            this.node.angle = this.data_angle
        }
    }
}


